﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using BestTrader.Cms.Models;
using BestTrader.Cms.IRepository;
using System.Text;
using BestTrader.Cms.Admin.Validation;
using FluentValidation.Results;
using BestTrader.Cms.ViewModels;
using BestTrader.Cms.Core.Helper;
using BestTrader.Cms.IServices;
using BestTrader.Cms.Core;
using System.Security.Claims;
using BestTrader.Cms.Admin.Filter;
using Microsoft.AspNetCore.Authorization;

namespace BestTrader.Cms.Admin.Controllers
{
    /// <summary>
    /// 返佣提现
    /// </summary>
    [Authorize]
    public class RebateExtractController : BaseController
    {
        public String UserId { get { return User.Claims.FirstOrDefault(x => x.Type == "UserId")?.Value; } }
        public String RoleId { get { return User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Role)?.Value; } }

        private readonly IRebateExtractService _service;
        private readonly IManagerService _managerService;

        public RebateExtractController(IRebateExtractService service, IManagerService managerService)
        {
            _service = service;
            this._managerService = managerService;
        }

        public IActionResult Index()
        {
            ViewData["AgentManagers"] = _managerService.GetSelfAndChildManagerByManagerIdAndRoleId(UserId, RoleId);

            return View();
        }


        public string LoadData([FromQuery]RebateExtractRequestModel model)
        {
            return JsonHelper.ObjectToJSON(_service.LoadData(model, UserId, RoleId));
        }

        [HttpGet]
        public IActionResult AddOrModify() {
            ViewData["TotalMoney"] = _service.GetTotalMoneyByManagerId(UserId);
            return View(); 
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public string AddOrModify([FromForm]RebateExtractAddOrModifyModel item)
        {
            var result = new BaseResult();
            RebateExtractValidation validationRules = new RebateExtractValidation();
            ValidationResult results = validationRules.Validate(item);
            if (results.IsValid)
            {
                result = _service.AddOrModify(item, UserId);
            }
            else
            {
                result.ResultCode = ResultCodeAddMsgKeys.CommonModelStateInvalidCode;
                result.ResultMsg = results.ToString("||");
            }
            return JsonHelper.ObjectToJSON(result);
        }

        //[HttpPost]
        //[ValidateAntiForgeryToken]
        //public string Delete(int[] roleId)
        //{
        //    return JsonHelper.ObjectToJSON(_service.DeleteIds(roleId));
        //}


    }
}